package cn.dada.dadarecipes.community.comment.service.impl;

import cn.dada.dadarecipes.base.exception.ServiceException;
import cn.dada.dadarecipes.base.response.StatusCode;
//import cn.dada.dadarecipes.base.util.LoginValidation;
import cn.dada.dadarecipes.community.article.mapper.ArticleMapper;
import cn.dada.dadarecipes.community.comment.mapper.CommentMapper;
import cn.dada.dadarecipes.community.comment.pojo.dto.CommentSaveParam;
import cn.dada.dadarecipes.community.comment.pojo.entity.Comment;
import cn.dada.dadarecipes.community.comment.pojo.vo.CommentVO;
import cn.dada.dadarecipes.community.comment.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleMapper articleMapper;



    /**
     * 业务层-查询评论列表
     * @param articleId
     * @return List<CommentVO>
     */
    @Override
    public List<CommentVO> selectComment(Long articleId) {
        log.debug("业务层-查询评论列表:{}", articleId);
        List<CommentVO> list = commentMapper.selectCommentByArticleId(articleId);
        log.debug("业务层-查询评论列表-结果数:{}", list.size());

        return list;
    }



    /**
     * 业务层-新增/更新文章评论
     * @param commentParam
     * @param session
     */
    @Override
    public void saveComment(CommentSaveParam commentParam, HttpSession session) {
        log.debug("业务层-新增/更新文章评论:{}", commentParam);
        /* 用户是否为当前用户登录校验 */
//        LoginValidation.isMyselfLogin(commentParam.getUserId(), session);

        /* 判断文章是否存在 */
        Long articleId = articleMapper.selectArticleId(commentParam.getArticleId());
        log.debug("业务层-添加评论-查询文章是否存在: {}",articleId);
        if (articleId == null) throw new ServiceException(StatusCode.ARTICLE_NOT_EXIST);// 文章不存在

        /* 判断是修改还是新增 */
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentParam, comment);
        LocalDateTime now = LocalDateTime.now();
        Boolean isSuccess;
        if (commentParam.getId() == null) {//新增
            comment.setCreateTime(now);
            isSuccess = commentMapper.insertComment(comment);
            articleMapper.updateArticleComment(comment.getArticleId(),+1);// 文章评论数+1
            log.debug("业务层-添加评论-新增评论是否成功: {}",isSuccess);
        }else {//修改
            comment.setUpdateTime(now);
            isSuccess = commentMapper.updateComment(comment);
            log.debug("业务层-添加评论-修改评论是否成功: {}",isSuccess);
        }
        if (!isSuccess) throw new ServiceException(StatusCode.OPERATION_FAILED);// 操作失败
    }



    /**
     * 业务层-删除评论  [只有文章作者或评论作者可以删除]
     * @param commentId
     * @param session
     */
    @Override
    public void deleteComment(Long commentId, HttpSession session) {
        log.debug("业务层-删除评论: {}", commentId);
        /* 用户是否为当前用户登录校验 */
//        Long userId = LoginValidation.isLogin(session);

        /* 获取当前评论信息 */
        Comment comment = commentMapper.selectCommentByCommentId(commentId);
        if (comment == null) throw new ServiceException(StatusCode.COMMENT_NOT_EXIST);// 评论不存在

//        /* 判断当前用户是否为文章作者或评论作者 */
//        Long articleUserId = articleMapper.selectArticleUserId(comment.getArticleId());
        // 判断当前用户是否为文章作者
//        if (articleUserId != null && !userId.equals(articleUserId) && !userId.equals(comment.getUserId())) {// 文章存在且不是作者且不是评论作者
//            throw new ServiceException(StatusCode.COMMENT_DEL_NOT_AUTHORIZED);// 没有权限删除
//        }

        /* 删除评论 */
        Boolean isSuccess = commentMapper.deleteComment(commentId);
        articleMapper.updateArticleComment(comment.getArticleId(),-1);// 文章评论数-1
        log.debug("业务层-删除评论-删除评论是否成功: {}",isSuccess);
        if (!isSuccess) throw new ServiceException(StatusCode.OPERATION_FAILED);// 操作失败
    }
}
